package com.douyin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douyin.dto.CommentAddDto;
import com.douyin.dto.Result;
import com.douyin.entity.Comments;
import com.douyin.entity.User;
import com.douyin.entity.Videos;
import com.douyin.mapper.CommentsMapper;
import com.douyin.mapper.UserMapper;
import com.douyin.mapper.VideosMapper;
import com.douyin.service.AuditProducerService;
import com.douyin.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyin.service.IFollowService;
import com.douyin.utils.BaseContext;
import com.douyin.vo.CommentVO;
import com.douyin.vo.TemptComment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 视频评论表 服务实现类
 * </p>
 *
 * @author k
 * @since 2025-07-27
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    VideosMapper videosMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditProducerService auditProducerService;
    @Override
    public Result addComment(CommentAddDto commentAddDto) {
        if (commentAddDto == null) {
            return Result.fail("评论失败");
        }
        Long videoId = commentAddDto.getVideoId();
        Videos videos = videosMapper.selectById(videoId);
        if (videos == null) {
            return Result.fail("视频不存在");
        }
        if (commentAddDto.getContent() == null || commentAddDto.getContent().isEmpty()) {
            return Result.fail("评论内容不能为空");
        }
        if (BaseContext.getCurrentId() == null) {
            return Result.fail("用户未登录");
        }
        Comments comments = new Comments();
        comments.setVideoId(videoId);
        comments.setUserId(BaseContext.getCurrentId());
        comments.setContent(commentAddDto.getContent());
        comments.setParentId(commentAddDto.getParentId());
        commentsMapper.insert(comments);
        User user = userMapper.selectById(BaseContext.getCurrentId());
        TemptComment temptComment = new TemptComment();
        BeanUtils.copyProperties(comments, temptComment);
        BeanUtils.copyProperties(user, temptComment);
        temptComment.setLikeCount(0);
        temptComment.setIsLiked(false);
        auditProducerService.sendAuditTask(comments.getId());
        return Result.ok(temptComment);
    }

    @Override
    public IPage<CommentVO> getTopComments(IPage<CommentVO> page, Long videoId, Long currentUserId) {
        Videos videos = videosMapper.selectById(videoId);
        if (videos == null) {
            return null;
        }
        if (currentUserId == null) {
            return null;
        }

        IPage<CommentVO> rootCommentsPage = commentsMapper.findRootCommentsPage(page, videoId, currentUserId);
        return rootCommentsPage;
    }

    @Override
    public List<CommentVO> getAllReplies(Long commentId, Long currentUserId,Long videoId) {
        // 调用递归查询获取所有子孙评论
        List<CommentVO> allDescendants = commentsMapper.findAllDescendants(videoId, commentId,currentUserId);

        // （可选）如果需要，可以在这里也组装成树形结构
        // 但通常前端拿到扁平列表后，根据parentId也能自行渲染
        // 这里我们直接返回扁平列表
        return allDescendants;
    }


}
